<?php
/*
 * @Author: your name
 * @Date: 2020-06-29 09:28:29
 * @LastEditTime: 2020-07-17 10:29:23
 * @LastEditors: 天羽落星辰
 * @Description: In User Settings Edit
 * @FilePath: \WWW\frx\application\api\controller\Index.php
 */ 

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 我的团队
 */
class Team extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 我的团队
     * @param string $limit  页数列表显示条数
     * @param string $paging 页数
     */ 
    public function index()
    {   
        $limit = intval($this->request->request('limit'));
        $paging = intval($this->request->request('paging'));
        $data = [];
        $limit = $limit ? $limit : 10;
        $paging = $paging ? 10 * $paging : 0;

        $data['team'] = $this->direct_user($limit,$paging);     
        // if($paging == 0){
        $data['count'] = $this->tpath_sum() + $this->auth->order_sum;
        $data['interposition_count'] = $this->interposition_count() - count($data['team']);
        // }


        $this->success('',$data);
    }

    // 统计我伞下（包括我自己）的正在接单总额度
    private function tpath_sum()
    {
        $order_sum = db('user')->where("FIND_IN_SET({$this->auth->id},tpath)")->sum('order_sum');
        return $order_sum;
    }

    // 我的间推人数 
    private function interposition_count()
    {   
        $interposition_count = db('user')->where("FIND_IN_SET({$this->auth->id},tpath)")->count();
        return $interposition_count;
    }
    // 我的全部直推用户 
    private function direct_user($limit,$paging)
    {      
        $direct_user = db('user')->field('username,level,f_level,mobile')->where('tid',$this->auth->id)->limit($paging,$limit)->select();
        if($direct_user){
            foreach($direct_user as &$v){
                $v['mobile'] = substr_replace($v['mobile'], '****', 3, 4); //2.替换字符串的子串
            }
        }
        return $direct_user;
    }
   
}
